package com.pe1n.yangtaopai.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.TwoTwoBean;
import com.pe1n.yangtaopai.page.detail.view.activity.DetailActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/11/28.
 */

public class MyActivityItemTwoItmRecyclerAdapter extends RecyclerView.Adapter<MyActivityItemTwoItmRecyclerAdapter.MyViewHolder> {
    private Context mContent;
    private List<TwoTwoBean.DataBean.ZranklistBean> intemOneListBean;
    private List<TwoTwoBean.DataBean.AllranklistBean> intemTwoListBean;
    private List<Object> list;
    private int[] top;
    private TypedArray ta;

    public MyActivityItemTwoItmRecyclerAdapter(Context mContent, List<Object> list) {
        this.mContent = mContent;
        this.list = list;
        this.ta=mContent.getResources().obtainTypedArray(R.array.top_list2);
        this.top=mContent.getResources().getIntArray(R.array.top_list2);
        for(int i=0;i<ta.length();i++){
            top[i]=ta.getResourceId(i,0);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContent).inflate(R.layout.activity_item_two_item_laout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (list.get(position) instanceof TwoTwoBean.DataBean.AllranklistBean) {
            TwoTwoBean.DataBean.AllranklistBean bean2 = (TwoTwoBean.DataBean.AllranklistBean) list.get(position);
            String urlString2 = bean2.getImagePath();
            Glide.with(mContent)
                    .load(urlString2)
                    .placeholder(R.drawable.place_holder)
                    .into(holder.loadimageView);
            holder.LoadTextXiangqing.setText(bean2.getProdName());
            holder.loadMoney.setText("¥" + bean2.getProdPrice());
            holder.loadXiaoliang.setText("上周日本销量" + bean2.getSalesCount());
            Glide.with(mContent)
                    .load(top[position])

                    .into(holder.image);
           // holder.image.setImageResource(top[position]);

        } else {
            TwoTwoBean.DataBean.ZranklistBean bean = (TwoTwoBean.DataBean.ZranklistBean) list.get(position);
            String urlString = bean.getImagePath();
            Glide.with(mContent)
                    .load(urlString)
                    .placeholder(R.drawable.place_holder)
                    .into(holder.loadimageView);
            holder.LoadTextXiangqing.setText(bean.getCName());
            holder.loadMoney.setText("¥" + bean.getProdPrice());
            holder.loadXiaoliang.setText("上周日本销量" + bean.getSalesCount());
            Glide.with(mContent)
                    .load(top[position])

                    .into(holder.image);
            //holder.image.setImageResource(top[position]);

        }



    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView loadimageView, image;
        private TextView LoadTextXiangqing, loadMoney, loadXiaoliang;

        public MyViewHolder(View itemView) {
            super(itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.get(getAdapterPosition()) instanceof TwoTwoBean.DataBean.ZranklistBean) {
                        TwoTwoBean.DataBean.ZranklistBean two = (TwoTwoBean.DataBean.ZranklistBean) list.get(getPosition());
                        Intent intent = new Intent(mContent, DetailActivity.class);
                        String id = two.getProdId();
                        String name = two.getProdName();
                        intent.putExtra("id", id);
                        intent.putExtra("name", name);
                        mContent.startActivity(intent);


                    } else if (list.get(getAdapterPosition()) instanceof TwoTwoBean.DataBean.AllranklistBean) {
                        TwoTwoBean.DataBean.AllranklistBean one = (TwoTwoBean.DataBean.AllranklistBean) list.get(getPosition());
                        Intent intent = new Intent(mContent, DetailActivity.class);
                        String id = one.getProdId();
                        String name = one.getProdName();
                        intent.putExtra("id", id);
                        intent.putExtra("name", name);
                        mContent.startActivity(intent);

                    }


                }
            });

            image = (ImageView) itemView.findViewById(R.id.tmage_Top1);
            loadimageView = (ImageView) itemView.findViewById(R.id.image_activity_item_two);
            LoadTextXiangqing = (TextView) itemView.findViewById(R.id.textview_activity_item_name);
            loadMoney = (TextView) itemView.findViewById(R.id.text_activity_item_money);
            loadXiaoliang = (TextView) itemView.findViewById(R.id.textview_activity_item_xiaoliang);
        }
    }
}
